import {BaseUrl_8098} from './baseapi.js'

var app = getApp();
module.exports = {
    uploadFile: params => {
        
        return new Promise((resolve, reject) => {
            console.log('上传图片:');
            console.log(params);
            wx.uploadFile({
                url: BaseUrl_8098() + "/api/v1/oss/upload_starx",
                filePath: params,
                name: 'file',
                formData: {},
                success: res => {
                    console.log('上传图片返回成功')
                    console.log(res.data)
                    res.data = JSON.parse(res.data)
                    if (res.statusCode === 200) {
                        if (res.data.code == 0) {
                            resolve(res.data);
                        } else {
                            wx.showModal({
                                title: '上传失败',
                                content: '请重新上传图片',
                                showCancel: false,
                                complete: () => {
                                    reject(res.data.msg);
                                },
                                success (res) {
                                    if (res.confirm) {
                                        
                                    } else if (res.cancel) {
                                        console.log('用户点击取消')
                                    }
                                }
                            });
                            wx.hideLoading()
                        }
                    } else {
                        wx.showModal({
                            title: '上传失败',
                            content: '请重新上传图片',
                            showCancel: false,
                            complete: () => {
                                reject(res.data.msg);
                            },
                            success (res) {
                                if (res.confirm) {
                                    
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        });
                        wx.hideLoading()
                    }
                },
                fail: err => {
                    if (err.errMsg === 'request:fail timeout') {
                    // 超时
                    }
                    wx.showModal({
                        title: '提示',
                        content: err,
                        showCancel: false,
                        complete: () => {
                            reject(err);
                        },
                        success (res) {
                            if (res.confirm) {
                            
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    });
                    wx.hideLoading()
                },
            })
        })
    }
};
